from flask_restful import Resource, reqparse,request
from ....Model.Models import User as UserModel
from ....utils import timestamp,response
from flask_restful_swagger import swagger

# user_poot_parser = reqparse.RequestParser()
# user_poot_parser.add_argument('name', type=str)
# user_poot_parser.add_argument('head_picture', type=str)
# user_poot_parser.add_argument('phone', type=str)
# user_poot_parser.add_argument('examine', type=str)
# user_poot_parser.add_argument('support', type=int)
# user_poot_parser.add_argument('password',type=str)
success = 1
notfond = -1
error = 0
class Userlist(Resource):
    def __init__(self, db, auth=None):
        self.db = db
    def get(self):#获取列表
        try:
            return response(success ,None, [each.as_dict() for each in self.db.session.query(UserModel).all()])
        
        except Exception as er:
            return response(error,er,None)

    def post(self):#添加
        try:
            args = user_poot_parser.parse_args()
            self.db.session.add(
                    UserModel(
                        name=args['name'],
                        head_picture=args['head_picture'],
                        phone =args['phone'],
                        examine=args['examine'],
                        support=args['support'],
                        password=args['password'],
                        last_login =timestamp(),
                        token = args['token'],
                        update_at=timestamp(),
                        create_at=timestamp()
                    )
                )
            self.db.session.commit
            return response(success,None,None)
        except Exception as er:
            return response(error,er,None)


    
class User(Resource):
    def __init__(self, db, auth=None):
        self.db = db

    def get(self,user_id):#查
        try:
            return response(success,None, UserModel.query.filter_by(id=user_id).first().as_dict())
        except Exception as er:
            return response(error,er,None)

    def delete(self,user_id):#删
        try:
            self.db.session.delete(
                UserModel.query.filter_by(id=user_id).first()
            )
            self.db.session.commit()
            return response(success,None,None)
        except Exception as er:
            return response(error,er,None)

    def put(self,user_id):#改
        try:
            user_obj = self.db.session.query(UserModel).filter_by(id=user_id).first()
            args = request.form
            if(args['name'] != None):
                user_obj.name = args['name']
            if(args['head_picture'] != None):
                user_obj.head_picture = args['head_picture']
            if(args['phone'] != None):
                user_obj.phone = args['phone']
            if(args['examine'] != None):
                user_obj.examine = args['examine']
            if(args['support'] != None):
                user_obj.support = args['support']
            if(args['password'] != None):
                user_obj.password = args['password']
            user_obj.update_at = timestamp()
            return response(success,None,None)
        except Exception as er:
            return response(error,er,None)
